﻿Public Class FrmFournisseur
    Dim position As Integer = 0

    Private Sub FrmFournisseur_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart
        Me.Left = 0
        Me.Top = 0

        MySQLConnect.marequete = "SELECT * FROM Fournisseur,travailleurindependant where idFournisseur=idFourn  "
        MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
        MySQLConnect.monremplisseur.SelectCommand = MySQLConnect.macommandespecialerequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "TousLesFournisseurs")
        dgvParticulier.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("TousLesFournisseurs")

        dgvParticulier.DataSource = (MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs"))


        MySQLConnect.marequete = "SELECT * FROM Fournisseur,entreprise where idFournisseur=idFourn  "
        MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
        MySQLConnect.monremplisseur.SelectCommand = MySQLConnect.macommandespecialerequete
        MySQLConnect.monremplisseur.Fill(MySQLConnect.monjeudedonnees, "TousLesFournisseurs2")
        DGVEntreprise.DataSource = MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2")

        DGVEntreprise.DataSource = (MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2"))

        gbEntreprise.Enabled = False
        gbParticulier.Enabled = False

        Rafraichir()
    End Sub


    Private Sub RechargerLesDonnees()
        MySQLConnect.MonJeuDeDonnees.Tables("TousLesFournisseurs").Clear()
        MySQLConnect.marequete = "SELECT * FROM Fournisseur,travailleurindependant where idFournisseur=idFourn"
        MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
        MySQLConnect.monremplisseur.SelectCommand = MySQLConnect.macommandespecialerequete
        MySQLConnect.monremplisseur.Fill(MySQLConnect.monjeudedonnees, "TousLesFournisseurs")


        MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2").Clear()
        MySQLConnect.marequete = "SELECT * FROM Fournisseur,entreprise where idFournisseur=idFourn"
        MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
        MySQLConnect.monremplisseur.SelectCommand = MySQLConnect.macommandespecialerequete
        MySQLConnect.monremplisseur.Fill(MySQLConnect.monjeudedonnees, "TousLesFournisseurs2")
    End Sub
    Public Sub Rafraichir()
        txtBoxNumero.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesFournisseurs").Rows(position).Item(0)
        txtBoxNom.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesFournisseurs").Rows(position).Item(1)
        txtBoxVille.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesFournisseurs").Rows(position).Item(2)
        txtBoxCodePostal.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesFournisseurs").Rows(position).Item(3)
        dgvParticulier.DataSource = (MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs"))

        txtBoxNumero.Text = MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2").Rows(position).Item(0)
        txtBoxNom.Text = MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2").Rows(position).Item(1)
        txtBoxVille.Text = MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2").Rows(position).Item(2)
        txtBoxCodePostal.Text = MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2").Rows(position).Item(3)
        DGVEntreprise.DataSource = (MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2"))



    End Sub



    Private Sub DGVFournisseur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txtBoxNumero.Text = dgvParticulier.CurrentRow.Cells(0).Value.ToString
        txtBoxNom.Text = dgvParticulier.CurrentRow.Cells(1).Value.ToString
        txtBoxVille.Text = dgvParticulier.CurrentRow.Cells(2).Value.ToString
        txtBoxCodePostal.Text = dgvParticulier.CurrentRow.Cells(3).Value.ToString

        txtBoxSiren.Text = ""

        cbParticulier.Checked = True
        gbParticulier.Enabled = True

        cbEntreprise.Checked = False
        gbEntreprise.Enabled = False
        btnAjouter.Text = "Ajouter"
    End Sub

    Private Sub FrmFournisseur_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs").Clear()
        MySQLConnect.monjeudedonnees.Tables("TousLesFournisseurs2").Clear()
    End Sub

    Private Sub cbEntreprise_Click(sender As Object, e As EventArgs) Handles cbEntreprise.Click
        cbParticulier.Checked = False
        gbEntreprise.Enabled = True
        gbParticulier.Enabled = False
    End Sub

    Private Sub cbParticulier_Click(sender As Object, e As EventArgs) Handles cbParticulier.Click
        cbEntreprise.Checked = False
        gbParticulier.Enabled = True
        gbEntreprise.Enabled = False
    End Sub





    Private Sub btnSupprimer_Click_1(sender As Object, e As EventArgs) Handles btnSupprimer.Click
        Dim id As Integer

        id = CInt(txtBoxNumero.Text)

        Dim message As String = "Etes vous sûr de vouloir l'effacer ?"
        Dim caption As String = "Supression du fournisseur !!"
        Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
        Dim icones As MessageBoxIcon = MessageBoxIcon.Exclamation
        Dim resultat As DialogResult
        resultat = MessageBox.Show(message, caption, Buttons)

        If resultat = System.Windows.Forms.DialogResult.Yes Then

            If cbParticulier.Checked = True Then

                MySQLConnect.marequete = "Delete from fournir Where idFrn = " & id & " "
                MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
                MySQLConnect.macommandespecialerequete.ExecuteNonQuery()

                MySQLConnect.marequete = "Delete from travailleurindependant  Where idFourn = " & id & " "
                MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
                MySQLConnect.macommandespecialerequete.ExecuteNonQuery()



            ElseIf cbEntreprise.Checked = True Then

                MySQLConnect.marequete = "Delete from vendre  Where idFrn = " & id & " "
                MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
                MySQLConnect.macommandespecialerequete.ExecuteNonQuery()

                MySQLConnect.marequete = "Delete from entreprise Where idFourn = " & id & " "
                MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
                MySQLConnect.macommandespecialerequete.ExecuteNonQuery()






            End If

            MySQLConnect.marequete = "Delete from fournisseur  Where idFournisseur = " & id & " "
            MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
            MySQLConnect.macommandespecialerequete.ExecuteNonQuery()


            btnAjouter.Text = "Ajouter"

            position = 0
            RechargerLesDonnees()
            Rafraichir()
        End If
    End Sub


    Private Sub btnAjouter_Click(sender As Object, e As EventArgs) Handles btnAjouter.Click
        Try
        Catch ex As Exception



        End Try
        If btnAjouter.Text = "Ajouter" And cbEntreprise.Checked = True Or btnAjouter.Text = "Ajouter" And cbParticulier.Checked = True Then

            MySQLConnect.Initialiser_TextBoxs(Me)
            txtBoxNumero.Text = MySQLConnect.GenererClePrimaire("idFournisseur", "Fournisseur")
            btnAjouter.Text = "Valider!"

        Else
            If txtBoxNom.Text <> "" And txtBoxCodePostal.Text <> "" And txtBoxVille.Text <> "" And txtBoxSiren.Text <> "" Or txtBoxNom.Text <> "" And txtBoxCodePostal.Text <> "" And txtBoxVille.Text <> "" And txtBoxTelephone.Text <> "" Then
                Dim id As Integer
                id = CInt(txtBoxNom.Text)
                Dim n, cp, v, tele, siren As String
                id = CInt(txtBoxNumero.Text)
                n = MySQLConnect.RemplaceApostrophesSlash(txtBoxNom.Text)
                v = txtBoxVille.Text
                cp = txtBoxCodePostal.Text
                tele = txtBoxTelephone.Text
                siren = txtBoxSiren.Text

                MySQLConnect.marequete = "Insert into Fournisseur Values ( " & id & ", '" & n & "', '" & v & "', '" & cp & "' )"
                MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
                MySQLConnect.macommandespecialerequete.ExecuteNonQuery()



                If cbEntreprise.Checked = True Then
                    MySQLConnect.marequete = "Insert into entreprise Values ( " & id & ",  '" & siren & "' )"
                    MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
                    MySQLConnect.macommandespecialerequete.ExecuteNonQuery()

                End If

                If cbParticulier.Checked = True Then
                    MySQLConnect.marequete = "Insert into travailleurindependant Values ( " & id & ",  '" & tele & "' )"
                    MySQLConnect.macommandespecialerequete.CommandText = MySQLConnect.marequete
                    MySQLConnect.macommandespecialerequete.ExecuteNonQuery()
                End If

                position = 0
                RechargerLesDonnees()
                Rafraichir()
                btnAjouter.Text = "Ajouter"

            Else
                MessageBox.Show("Renseigner tous les champs SVP...!", "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If


    End Sub

    Private Sub btnModifier_Click_1(sender As Object, e As EventArgs) Handles btnModifier.Click

    End Sub


    Private Sub DGVParticulier_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEntreprise.CellContentClick
        txtBoxNumero.Text = DGVEntreprise.CurrentRow.Cells(0).Value.ToString
        txtBoxNom.Text = DGVEntreprise.CurrentRow.Cells(1).Value.ToString
        txtBoxVille.Text = DGVEntreprise.CurrentRow.Cells(2).Value.ToString
        txtBoxCodePostal.Text = DGVEntreprise.CurrentRow.Cells(3).Value.ToString
        txtBoxSiren.Text = DGVEntreprise.CurrentRow.Cells(6).Value.ToString


        txtBoxTelephone.Text = ""
        cbParticulier.Checked = False
        gbParticulier.Enabled = False

        cbEntreprise.Checked = True
        gbEntreprise.Enabled = True
        btnAjouter.Text = "Ajouter"
    End Sub



    Private Sub dgvEntreprise_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVParticulier.CellContentClick
        txtBoxNumero.Text = dgvParticulier.CurrentRow.Cells(0).Value.ToString
        txtBoxNom.Text = dgvParticulier.CurrentRow.Cells(1).Value.ToString
        txtBoxVille.Text = dgvParticulier.CurrentRow.Cells(2).Value.ToString
        txtBoxCodePostal.Text = dgvParticulier.CurrentRow.Cells(3).Value.ToString
        txtBoxTelephone.Text = dgvParticulier.CurrentRow.Cells(4).Value.ToString

        txtBoxSiren.Text = ""

        cbEntreprise.Checked = False
        gbEntreprise.Enabled = False

        cbParticulier.Checked = True
        gbParticulier.Enabled = True
        btnAjouter.Text = "Ajouter"
    End Sub
End Class